package com.yonyou.gmmc.service.points.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yonyou.gmmc.service.points.dao.TmCarRecommendPointsMapper;
import com.yonyou.gmmc.service.points.dao.TmPointsRuleMapper;
import com.yonyou.gmmc.service.points.model.TmCarRecommendPoints;
import com.yonyou.gmmc.service.points.model.TmPointsRule;

import tk.mybatis.mapper.entity.Example;

@Service("pointsRuleService")
@Transactional
public class PointsRuleService {

	@Autowired
	private TmPointsRuleMapper tmPointsRuleMapper;
	
	@Autowired
	private TmCarRecommendPointsMapper tmCarRecommendPointsMapper;

	/**
	 * 根据业务类型获取积分规则
	 * 
	 * @param BizType
	 * @param BizStep
	 * @return
	 */
	public TmPointsRule getRuleByBIZ(Long BizType,Long BizStep){
		TmPointsRule rule = new TmPointsRule();
		rule.setBcId(BizType);
		rule.setTcId(BizStep);
		return tmPointsRuleMapper.selectOne(rule);
	}
	
	/**
	 * 根据ID获取积分规则
	 * 
	 * @param BizType
	 * @param BizStep
	 * @return
	 */
	public TmPointsRule getRuleByID(Long id){
		return tmPointsRuleMapper.selectByPrimaryKey(id);
	}
	
	/**
	 * 根据推荐成功的数量获取要获取的积分
	 * 
	 * @return TmCarRecommendPoints
	 */
	public TmCarRecommendPoints getPointsByNum(int num) {
		Example example = new Example(TmCarRecommendPoints.class);
		example.createCriteria().andGreaterThanOrEqualTo("volumeTo", num).andLessThanOrEqualTo("volumeFrom", num);
		List<TmCarRecommendPoints> pointsList = tmCarRecommendPointsMapper.selectByExample(example);
		
		if(pointsList.size()==1){
			return pointsList.get(0);
		}else{
			return null;
		}
	}
	
}
